package com.qxzn.app.medicalhealth.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.qxzn.app.bean.RepeatDateBean;
import com.qxzn.app.bean.BaseRepeatDateBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.medicalhealth.adapter.FamilyMedicalRecordsContentAdapter;
import com.qxzn.app.medicalhealth.util.ScalePageTransformer;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.util.AppUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 编辑时间周期
 */
public class AddRepeatDateActivity extends BaseActivity {

    @BindView(R2.id.ll_tab)
    LinearLayout llTab;
    @BindView(R2.id.vp_content)
    ViewPager vpContent;
    private FamilyMedicalRecordsContentAdapter mAdapter1;
    private List<RepeatDateBean> dataList = new ArrayList<>();
    private int currentPosition;
    private String value, value0 = "1", value1 = "1", value2 = "1";
    private String repeatData = "[{\"title\":\"每天\",\"type\":0,\"data\":[{\"id\":1,\"name\":\"每天\",\"check\":\"true\"}]},{\"title\":\"每周\",\"type\":1,\"data\":[{\"id\":1,\"name\":\"周一\",\"check\":\"true\"},{\"id\":2,\"name\":\"周二\"},{\"id\":3,\"name\":\"周三\"},{\"id\":4,\"name\":\"周四\"},{\"id\":5,\"name\":\"周五\"},{\"id\":6,\"name\":\"周六\"},{\"id\":7,\"name\":\"周日\"}]},{\"title\":\"每隔几天\",\"type\":2,\"data\":[{\"id\":1,\"name\":\"1\"}]}]";

    @Override
    protected int initLayout() {
        return R.layout.activity_add_repeat_date;
    }

    @Override
    protected void initView() {
//        vpContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> setStatusBar());
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("编辑时间周期");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        dataList = JSON.parseArray(repeatData, RepeatDateBean.class);
        for (int i = 0; i < dataList.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_tab_repeat, null, false);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.weight = 1;
            view.setLayoutParams(lp);
            TextView tv_tab_name = view.findViewById(R.id.tv_tab_name);
            View view_tab = view.findViewById(R.id.view_tab);
            tv_tab_name.setText(dataList.get(i).getTitle());
            dataList.get(i).setTvTab(tv_tab_name);
            dataList.get(i).setViewLine(view_tab);
            final int position = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vpContent.setCurrentItem(position, true);
                }
            });
            llTab.addView(view);
        }
        setCheckTab(currentPosition);
        vpContent.setPageTransformer(true, new ScalePageTransformer());
        mAdapter1 = new FamilyMedicalRecordsContentAdapter(this, AddRepeatDateActivity.class.getSimpleName());
        mAdapter1.setData(dataList);
        //在设置数据之后必须得调setAdapter，否则会导致Transform初始状态不正常
        vpContent.setAdapter(mAdapter1);
        vpContent.setCurrentItem(currentPosition, true);
        vpContent.setOffscreenPageLimit(dataList.size() - 1);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCheckTab(position);
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setCheckTab(int position) {
        for (int i = 0; i < dataList.size(); i++) {
            if (i == position) {
                dataList.get(i).getTvTab().setTextColor(getResources().getColor(R.color.main_blue));
                dataList.get(i).getViewLine().setVisibility(View.VISIBLE);
            } else {
                dataList.get(i).getTvTab().setTextColor(getResources().getColor(R.color.black_2A2A2A));
                dataList.get(i).getViewLine().setVisibility(View.INVISIBLE);
            }
        }
    }

    @OnClick(R2.id.tv_save)
    public void onViewClicked() {
        RepeatDateBean repeatDateBean = dataList.get(currentPosition);
        if (currentPosition == 1) {
            List<BaseRepeatDateBean.DataBean> list = repeatDateBean.getData();
            boolean select = false;
            for (BaseRepeatDateBean.DataBean bean : list) {
                if (bean.isCheck()) {
                    select = true;
                    break;
                }
            }
            if (!select) {
                AppUtil.showToast(this, "请选择周几提醒您");
                return;
            }
        }
        BaseRepeatDateBean baseRepeatDateBean = new BaseRepeatDateBean();
        baseRepeatDateBean.setType(repeatDateBean.getType());
        baseRepeatDateBean.setTitle(repeatDateBean.getTitle());
        baseRepeatDateBean.setData(repeatDateBean.getData());

        Intent intent = new Intent();
        intent.putExtra("data", baseRepeatDateBean);
        setResult(RESULT_OK, intent);
        finish();
    }

}
